﻿using MediatR;
using PersonnelManagement.Application.Command;
using PersonnelManagement.Domain.Dto;
using PersonnelManagement.Infrstructre.Interface;

namespace PersonnelManagement.Application.CommandHandler
{
    public class statisticsClientCommandHandlers : IRequestHandler<statisticsClientCommand, List<statisticsClientDtos>>
    {
        IClientRepository cRep;
        protected readonly IClientcategoryRepository ctRep;

        public statisticsClientCommandHandlers(IClientRepository cRep, IClientcategoryRepository ctRep)
        {
            this.cRep = cRep;
            this.ctRep = ctRep;
        }

        public async Task<List<statisticsClientDtos>> Handle(statisticsClientCommand request, CancellationToken cancellationToken)
        {
            var list = (await cRep.Getall()).ToList();
            var result = (await ctRep.Getall()).ToList();
            var newlist = from a in list
                          join b in result on a.CId equals b.CId
                          group b by new { b.CName } into g
                          select new statisticsClientDtos
                          {
                              CName = g.Key.CName,
                              CNum = g.Count()
                          };
            return newlist.ToList();
        }
    }
}
